<?php
namespace App\Service\Admin;

class SaleService extends BaseService
{
    //销售

    //平台类型
    protected static $plat_types = [
        1 => '美团', '饿了么', '京东',
    ];

    //列表
    public static function index($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwSale)->where('del', 0);
        if ($pas['shop_id']) {
            $db = $db->where('shop_id', $pas['shop_id']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $types = self::$plat_types;
        $arys = [];
        foreach ($res as $v) {;
            $plat_name = array_key_exists($v->plat_type, $types) ? $types[$v->plat_type] : '-';
            $orderno = $v->orderno ? $v->orderno : '-';
            $intime = $v->intime ? $v->intime : '-';
            $outtime = $v->outtime ? $v->outtime : '-';
            $shopOne = self::dbConn(self::$dbYwShop)
                ->where('id', $v->shop_id)
                ->first();
            $shop_name = empty($shopOne) ? '' : $shopOne->title;
            $arys[] = [
                'id' => $v->id, 'orderno' => $orderno,
                'plat_type' => $v->plat_type, 'plat_name' => $plat_name,
                'intime' => $intime, 'outtime' => $outtime,
                'sale_num' => toNumber($v->sale_num), 'profit' => toNumber($v->profit),
                'stock' => $v->stock, 'stock_base' => $v->stock_base,
                'shop_id' => $v->shop_id, 'shop_name' => $shop_name,
                'lasttime' => $v->uptime,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //新增
    public static function add($pas)
    {
        $pas['orderno'] = toCode();
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwSale)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //新增
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbYwSale)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwSale)->where('id', $id)->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbYwSale)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $res = self::dbConn(self::$dbYwSale)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}